﻿using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using System;
using ESRI.ArcGIS.Carto;

namespace Akiha.Core
{
    public static class SymbolWrapper
    {
        public static ISymbol GetDefaultSymbol(esriGeometryType geometryType)
        {
            IRgbColor randomColor = new RgbColorClass();
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            randomColor.Red = rd.Next(0, 255);
            randomColor.Green = rd.Next(0, 255);
            randomColor.Blue = rd.Next(0, 255);
            return GetSpecifyColorSymbol(geometryType, randomColor);
        }

        public static ISymbol GetSpecifyColorSymbol(esriGeometryType geometryType, IColor color)
        {
            ISymbol symbol = null;
            switch (geometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    {
                        ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass
                        {
                            Color = color,
                            Size = 2.0,
                            Style = esriSimpleMarkerStyle.esriSMSCircle
                        };
                        return (markerSymbol as ISymbol);
                    }
                case esriGeometryType.esriGeometryMultipoint:
                    return symbol;

                case esriGeometryType.esriGeometryPolyline:
                case esriGeometryType.esriGeometryLine:
                    {
                        ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass
                        {
                            Color = color,
                            Width = 1.0,
                            Style = esriSimpleLineStyle.esriSLSSolid
                        };
                        return (lineSymbol as ISymbol);
                    }
                case esriGeometryType.esriGeometryPolygon:
                    {
                        ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass
                        {
                            Color = color
                        };
                        fillSymbol.Outline.Color = RgbColorWrapper.GetRGBColor(255, 255, 0,255);
                        fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
                        return (fillSymbol as ISymbol);
                    }
            }
            return symbol;
        }

        public static IColor GetSymbolColor(ISymbol symbol)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (symbol is ILineSymbol)
            {
                return (symbol as ILineSymbol).Color;
            }
            if (symbol is IMarkerSymbol)
            {
                return (symbol as IMarkerSymbol).Color;
            }
            if (!(symbol is IFillSymbol))
            {
                throw new NotImplementedException("Not supported symbol.");
            }
            return (symbol as IFillSymbol).Color;
        }

        public static IElement GetPointTextElement(IGeometry geometry, string text, float size, double angle)
        {
            IElement element;
            ITextElement textElement = new TextElementClass();
            element = textElement as IElement;
            ITextSymbol textSymbol = new TextSymbolClass();
            textSymbol.Color = RgbColorWrapper.GetRGBColor(255, 0, 0,255);
            textSymbol.Size = Convert.ToDouble(size);
            //角度参数貌似没起作用


            textSymbol.Angle = angle;
            //textSymbol.Font = ;
            //textSymbol.HorizontalAlignment = GetHorizontalAlignment();
            //textSymbol.VerticalAlignment = GetVerticalAlignment();
            element.Geometry = geometry;
            textElement.Symbol = textSymbol;
            textElement.Text = text;

            return element;
        }





    }
}

